<?php
/**
 * 文件描述  模型基类
 * =================================================================
 * 文件名称  Model.class.php
 * -----------------------------------------------------------------
 * 作    者  YYT<375776626@vip.qq.com>
 * -----------------------------------------------------------------
 * 创建时间  2010-1-19
 * -----------------------------------------------------------------
 * 版权信息  创思工作室
 * =================================================================
 */

class Model
{
    private $_db;
    protected $_table;
    protected $_config;

    public function __construct()
    {
        global $config;
        $this->_config = $config;
        $this->_table = $config['db']['prefix'] . $this->_table;
    }

    private function __call($name, $args)
    {
        /* 扩展数据库方法 */
        if (in_array(strtolower($name), array('table', 'where', 'order', 'limit', 'field', 'group', 'having'))) {
            $this->_args[$name] = array_shift($args);
        } else if ($name == 'join') {
            $this->_args['join'][] = array_shift($args);
        /* 扩展缓存方法 */
        } else if ($name == 'Cache') {
            $Cache = new Cache();
            return $Cache->__input($this, $args);
        }
        return $this;
    }

    private function dbConnect()
    {
        /* 判断是否已被加载 */
        if (!isset($this->_db)) {
            /* 根据数据库类型选择 */
            switch (strtolower($this->_config['db']['type'])) {
                case 'mysql';
                require CS_PATH . 'Lib/Db/Mysql.class.php';
                $this->_db = new Mysql();
                $this->_db->connect($this->_config['db']['host'], $this->_config['db']['user'], $this->_config['db']['password'], $this->_config['db']['name'], $this->_config['db']['pconnect'], $this->_table, $this->_config['db']['return']);
                break;

                case 'sqlite';
                break;
            }
        }
    }

    /**
     * SQL语句查询
     * @access  protected
     * @param   string  $sql  SQL语句
     * @param   查询类型 "ALL" 返回所有
     * @return  结果集
     */
    protected function query($sql)
    {
        $this->dbConnect();
        return $this->_db->query($sql);
    }

    /**
     * 执行SQL语句
     * @access  protected
     * @param   SQL语句
     */
    protected function execute($sql)
    {
        $this->dbConnect();
        return $this->_db->execute($sql);
    }

    /**
     * 查询数据
     * @access  protected
     * @param   字段名称
     * @return  结果集
     */
    protected function find($field = '*', $type = '')
    {
        $this->dbConnect();
        $this->_args['field'] = isset($this->_args['field']) ? $this->_args['field'] : $field;
        $result = $type === 'ALL' ? $this->_db->findAll($this->_args) : $this->_db->find($this->_args);
        return $result;
    }

    /**
     * 增加一条数据
     * @access  protected
     * @param   array('字段名称' => '值')
     */
    protected function insert($data = array())
    {
        $this->dbConnect();
        $this->_db->insert($data);
        return $this->_db->insertId();
    }

    /**
     * 更新数据
     * @access  protected
     * @param   array('字段名称' => '值')->where('条件')
     */
    protected function update($data = array())
    {
        if ($this->args['where']) {
            $this->dbConnect();
            return $this->_db->update($data, $this->args['where']);
        }
    }

    /**
     * 删除数据
     * @access  protected
     * @param   string  条件
     */
    protected function delete($where)
    {
        return $this->_db->delete($where);
    }
}